package com.permission.systempermission.comm.exception;

import com.permission.systempermission.comm.Result.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = RuntimeException.class)
    public ResultVo handler(RuntimeException e){
        log.error("运行时异常：----------------{}", e.getMessage());
        System.out.println("运行时异常：");
        return ResultVo.error(e.getMessage());
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResultVo handleMaxSizeException() {
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("timestamp", LocalDateTime.now());
        body.put("status", HttpStatus.PAYLOAD_TOO_LARGE.value());
        body.put("error", "文件过大");
        body.put("message", "单个文件不能超过10MB");
        body.put("maxAllowedSize", "10MB");
        return ResultVo.error("上传失败",body);
    }


}
